<template>
  <div class="pt-96 bg-[#1a2c38] w-full min-375 h-full">
    <div class="w-full" id="iframeId">
      <iframe
        :src="iframeSrc"
        frameborder="0"
        style="width: 100%"
        :style="{ height: innerHeight + 'px' }"
        sandbox="allow-scripts allow-same-origin allow-forms allow-popups allow-top-navigation"
      ></iframe>
      <!-- <div class="container">
        <more />
      </div> -->
    </div>
  </div>
</template>

<script setup name="Tools">
import { ref, getCurrentInstance, onActivated } from "vue";
import { getGameSportCode, gameLogin } from "../../api/casion.js";
import { useRouter, useRoute } from "vue-router";
import { eventBus } from "@/utils/eventBus";
import { mainAct } from "@/store/activeda";

const router = useRouter();
const {
  appContext: {
    config: { globalProperties }
  }
} = getCurrentInstance();
const main2 = mainAct();

const main = mainAct();

const innerHeight = ref(0);
onActivated(() => {
  innerHeight.value = window.innerHeight - 66;
  gotoGame();
});

const iframeSrc = ref("");
const gotoGame = () => {
  if (!main2.isLogin) {
    if (main2._isMobile) {
      router.push("/login");
    } else {
      main2.showLoginLayer("no");
    }
    return;
  }
  getGameSportCode({ business_code: "tss" }).then(res => {
    if (res.errno == 0) {
      let data = {
        game_code: res.data.tss
      };
      main.currentGame.code = res.data.tss;
      gameLogin(data).then(rows => {
        main2.isLoading = false;
        if (rows.errno == 0) {
          //跳转iframe
          eventBus.emit("game_enter", data.game_code);
          iframeSrc.value = rows.data.path;
          // const container = document.getElementById("iframeId");
          // // 动态创建 iframe
          // const iframe = document.createElement("iframe");
          // iframe.src = rows.data.path; // 设置 URL
          // iframe.frameBorder = 0; // 去掉边框
          // iframe.className = "w-full h-full"; // 设置类名
          // let heights = innerHeight + "px";
          // iframe.style.height = "700px";
          // iframe.sandbox =
          //   "allow-scripts allow-same-origin allow-forms allow-popups";
          // iframe.allow = "fullscreen"; // 添加允许权限，根据需要扩展
          // iframe.onload = () => console.log("Iframe loaded successfully");
          // iframe.onerror = () => console.error("Failed to load iframe.");
          // container.appendChild(iframe); // 添加到容器
          console.log("rows.data.path", iframeSrc.value, rows.data.path);

          // router.push({
          //   name: "Iframe",
          //   query: {
          //     game_url: rows.data.path,
          //     userToken: main2.user_detail.uid
          //   }
          // });
          // if (rows.data.coupon_disable) {
          //   ElNotification({
          //     position: "top-left",
          //     zIndex: 1004,
          //     title: "",
          //     message: globalProperties.$t("page.casino.sportsTip"),
          //     type: "success"
          //   });
          // }

          // coupon_disable == 1 显示
          // 活动获取的金额不能在体育下注，体育游戏余额将会扣除活动金额。

          // wallet_rate = 1 显示
          // 投注NGN游戏将会为您的余额价值提升100倍，您可能会在游戏中看到您的余额缩小
          if (rows.data.coupon_disable) {
            ElNotification({
              position: "top-left",
              zIndex: 1004,
              title: "",
              message: globalProperties.$t("page.casino.sportsTip"),
              type: "success"
            });
          }

          if (rows.data.wallet_rate != 1) {
            ElNotification({
              position: "top-left",
              zIndex: 1004,
              title: "",
              message: globalProperties.$t("page.casino.wallet_rate"),
              type: "success"
            });
          }

          // window.open(rows.data.path, "_blank");
        } else if (
          rows.errno == 10015 ||
          rows.errno == 20005 ||
          rows.errno == 10204
        ) {
          // console.log(10015);
          ElNotification({
            position: "top-left",
            zIndex: 1004,
            title: "Error",
            message: rows.msg,
            type: "error"
          });
          main2.loginLayer = true;
        }
      });
    }
  });
};
</script>
<style scoped lang="less">
.my-swipe {
  width: 80vw;

  margin: auto;
  margin-bottom: 2rem;
  .van-swipe-item {
    color: #fff;
    font-size: 20px;
    line-height: 150px;
    text-align: center;
    img {
      width: 80vw;
      max-width: 1220px;
      min-width: 500px;
    }
  }
}
.nav-item {
  color: #ffffff;
  padding: 10px 20px;
  cursor: pointer;
  white-space: nowrap;
}
.nav-item:hover {
  background-color: #2f4553;
  border-radius: 20px;
}
.nav-item.active {
  background-color: #2f4553;
  border-radius: 20px;
}
.container {
  overflow: hidden;
}
@media (max-width: 600px) {
  .game-card {
    width: 30% !important;
    max-width: unset;
  }
  .banner {
    height: 80px !important;
    line-height: 80px !important;
  }
  .gameBox {
    grid-gap: 10px 5px !important;
    grid-template-columns: repeat(3, 1fr) !important;
    .item {
      width: 164px;
      max-width: unset;
      min-width: unset;
    }
  }
}
.game-card:hover {
  transform: translateY(-4%);
}
.game-card {
  max-width: 200px;
  width: 200px;
  height: 250px;
  cursor: pointer;
  background-color: #667386;
  border-radius: 10px;
  /* padding: 20px; */
  text-align: center;
  margin: 10px;
  /* flex: 1 0 21%; Adjust the flex-basis to control the number of items per row */
  position: relative;
  will-change: transform;
  transition: 0.3s;
  .game-card-con {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    img {
      width: auto;
      min-width: 200px;
      border-radius: 10px;
    }
    p {
      margin-top: 10px;
      color: #ffffff;
    }
    .players {
      color: #00ff00;
    }
  }
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.pagination button {
  background-color: #1c2a38;
  color: #ffffff;
  border: none;
  padding: 10px 20px;
  margin: 0 5px;
  cursor: pointer;
  border-radius: 5px;
  border: 1px solid #2f4453;
}
.pagination button:first-child {
  margin-right: 0;
  border-radius: 40% 0 0 40%;
  border-right: 0;
}
.pagination button:last-child {
  margin-left: 0;
  border-radius: 0 40% 40% 0;
}
.pagination button.disabled {
  background-color: #1a2c38;
  color: #667386;
  cursor: not-allowed;
}
.banner {
  color: #fff;
  font-weight: 600;
  font-size: 20px;
  height: 115px;
  line-height: 115px;
}
.gameBox {
  background-color: #1a2c38;
  display: grid;
  grid-gap: 2rem 1rem;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  .item {
    width: 156px;
    color: #fff;
    background-color: #667386;
    text-align: center;
    line-height: 160px;
    border-radius: 4px;
    cursor: pointer;
    will-change: transform;
    transition: 0.3s;
    border-radius: 8px;

    img {
      width: 100%;
      height: 100%;
      border-radius: 8px;
    }
  }
  .item:hover {
    transform: translateY(-4%);
  }
}

.soccer_box {
  width: 100%;
  color: #fff;
  margin-top: 6px;
  display: flex;
  align-items: center;
  justify-content: space-around;

  .soccer_box1 {
    position: relative;
    margin-right: 10px;

    .absolute1 {
      grid-gap: 10px 5px !important;
      grid-template-columns: repeat(3, 1fr) !important;
      position: absolute;
      left: 2rem;

      .num_text {
        font-weight: 800;
        font-size: 40px;
        margin-top: 0.8rem;
      }
      .name_qiu {
        font-weight: 400;
        font-size: 18px;
        margin-top: 0.8rem;
      }
      .name_en {
        font-weight: 200;
        font-size: 18px;
        margin-top: 0.8rem;
      }
    }
  }
}

.displa_nnone_img {
  display: none;
}

@media (max-width: 600px) {
  .soccer_box {
    width: 100%;
    display: flex;
    align-items: center;
    flex-direction: column;
    // justify-content: space-around;
    position: relative;
    .displa_nnone {
      display: none;
    }
    .displa_nnone_img {
      display: block;
    }
    .soccer_box1 {
      position: relative;
      margin-bottom: 10px;
      margin-right: 0px;
      .img {
        width: 100%;
        height: auto;
      }
      .absolute1 {
        position: absolute;
        top: 20px;
        left: 50px;
        .num_text {
          font-weight: 700;
          font-size: 24px;
        }
        .name_qiu {
          font-weight: 400;
          font-size: 16px;
          margin-top: 10px;
        }
        .name_en {
          font-weight: 200;
          font-size: 14px;
          margin-top: 10px;
        }

        .munber {
          margin-top: 20px;
        }

        .name_ {
          margin-top: 22px;
          margin-bottom: 30px;
        }
      }
    }

    .phone_img {
    }
  }

  .gameBox {
    width: 100%;
    .item {
      width: 100%;
    }
  }
}
</style>
